#include "../include/utilities.h"

#include <random>
#include <ranges>

#include "raylib.h"

Vector3 Polar3DToXYZRandom(Polar3D polar) {
	float RadAlpha = DEG2RAD * polar.alpha;
	float RadBeta = DEG2RAD * polar.beta;
	std::random_device rd;  // 将用于获得随机数引擎的种子
	std::mt19937_64 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
	std::normal_distribution<> d{5, 10};
	float z = polar.length * sin(RadAlpha) + d(gen);
	float xy = polar.length * cos(RadAlpha);
	float x = xy * cos(RadBeta) + d(gen);
	float y = xy * sin(RadBeta) + d(gen);
	return {x, y, z};
}

Vector3 Polar3DToXYZ(Polar3D polar) {
	float RadAlpha = DEG2RAD * polar.alpha;
	float RadBeta = DEG2RAD * polar.beta;
	float z = polar.length * sin(RadAlpha);
	float xy = polar.length * cos(RadAlpha);
	float x = xy * cos(RadBeta);
	float y = xy * sin(RadBeta);
	return {x, y, z};
}

std::pair<Vector3, Vector3> getOtherNormal(Vector3 normal) {
	Vector3 a = Vector3CrossProduct(normal, {1, 0, 0});
	if (a.x == 0 && a.y == 0 && a.z == 0) {
		a = Vector3CrossProduct(normal, {0, 1, 0});
	}
	Vector3 b = Vector3CrossProduct(normal, a);
	a = Vector3Normalize(a);
	b = Vector3Normalize(b);
	return {a, b};
}

// 获取给定圆心、半径、点数的同z平面圆
std::vector<Vector3> genCirclePos(Vector3 center, float radius, uint8_t posCount) {
	float theta = 0;
	float deltTheta = 360. / posCount;
	std::vector<Vector3> res{posCount};
	for(auto i : std::views::iota(0) | std::views::take(posCount)) {
		float radTheta = DEG2RAD * theta;
		float x = center.x + radius * sin(radTheta);
		float y = center.y + radius * cos(radTheta);
		float z = center.z;
		res[i] = {x, y, z};
		theta += deltTheta;
	}
	return res;
}

// 获取给定任意法向量以及圆心、半径的圆离散点
std::vector<Vector3> getNormalCirclePos(Vector3 circlePos, float radius, Vector3 normal, float startAngle, float endAngle, uint8_t pointCount) {
	std::vector<Vector3> res{pointCount};
	auto [a, b] = getOtherNormal(normal);
	float deltAngle = (endAngle - startAngle) / pointCount;
	float alphaDeg = startAngle;
	Vector3 pointPos;
	float alpha;
//	for (int i = 0; i < pointCount; ++i) {
//		alpha = DEG2RAD * alphaDeg;
//		pointPos.x = circlePos.x + radius * a.x * cosf(alpha) + radius * b.x * sinf(alpha);
//		pointPos.y = circlePos.y + radius * a.y * cosf(alpha) + radius * b.y * sinf(alpha);
//		pointPos.z = circlePos.z + radius * a.z * cosf(alpha) + radius * b.z * sinf(alpha);
//		alphaDeg += deltAngle;
//		res[i] = pointPos;
//	}
	for(auto i : std::views::iota(0) | std::views::take(pointCount)) {
		alpha = DEG2RAD * alphaDeg;
		pointPos.x = circlePos.x + radius * a.x * cosf(alpha) + radius * b.x * sinf(alpha);
		pointPos.y = circlePos.y + radius * a.y * cosf(alpha) + radius * b.y * sinf(alpha);
		pointPos.z = circlePos.z + radius * a.z * cosf(alpha) + radius * b.z * sinf(alpha);
		alphaDeg += deltAngle;
		res[i] = pointPos;
	}
	return res;
}

std::vector<Vector3> getRosebushPos(Vector3 rosebushPos, Vector3 normal, float radius, uint8_t bushCount) {
	auto [normalX, normalY] = getOtherNormal(normal);
	Matrix modelCoordinateAxisBase = {
		normalX.x, normalY.x, normal.x, 0,
		normalX.y, normalY.y, normal.y, 0,
		normalX.z, normalY.z, normal.z, 0,
		0, 0, 0, 1
	};

	// r = cos(a*theta);
	// x = r*cos(theta)
	// y = r*sin(theta)
	// z = a/2*sin(a*theta) Or z = r*sin(a*theta) Or z = 0
	int pointCount = 40 * bushCount;
	float deltTheta = 360.f / pointCount;
	float theta = 0;
	std::vector<Vector3> res;
	Vector3 pointPos;
	float thetaRad;
	for (auto i : std::views::iota(0) | std::views::take(pointCount)) {
		thetaRad = DEG2RAD * theta;
		theta += deltTheta;
		float r = radius * cosf(bushCount * thetaRad);
		pointPos = {r * cosf(thetaRad), r * sinf(thetaRad), 0};
		pointPos = Vector3Transform(pointPos, modelCoordinateAxisBase);
		pointPos = Vector3Add(pointPos, rosebushPos);
		res.push_back(pointPos);
	}
	
	return res;
}

std::vector<Vector3> getNormalConePos(Vector3 circlePos, Vector3 vertexPos, float radius) {
	std::random_device rd;  // 将用于获得随机数引擎的种子
	std::mt19937_64 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
	std::uniform_int_distribution<> heightCountDis(-10, 10);
	std::uniform_int_distribution<> alphaCountDis(10, 20);
	std::vector<Vector3> res;
	Vector3 normal = Vector3Subtract(vertexPos, circlePos);
	Vector3 normalizationNormal = Vector3Normalize(normal);
	auto [a, b] = getOtherNormal(normalizationNormal);
	float height = Vector3Length(normal);
	uint8_t heightCount = 20 + heightCountDis(gen);
	uint8_t circleCount = 20 + alphaCountDis(gen);
	float deltHeight = height / (heightCount - 1);
	float deltAngle = 360.0f / circleCount;
	float angle = 0.0f;
	float heightNow = 0.0f;
	Vector3 pointPos;
	for (int i = 0; i < heightCount; ++i) {
		for (int j = 0; j < circleCount; ++j) {
			float alpha = DEG2RAD * angle;
			pointPos.x = circlePos.x + radius * a.x * cosf(alpha) + radius * b.x * sinf(alpha);
			pointPos.y = circlePos.y + radius * a.y * cosf(alpha) + radius * b.y * sinf(alpha);
			pointPos.z = circlePos.z + radius * a.z * cosf(alpha) + radius * b.z * sinf(alpha);
			angle += deltAngle;
			res.push_back(pointPos);
		}
		heightNow += deltHeight;
		circlePos = Vector3Add(circlePos, Vector3Scale(normalizationNormal, heightNow));
		radius = radius * (height - heightNow) / height;
	}
	return res;
}

Vector3 getRandomNormal(float startAlpha, float endAlpha, float startBeta, float endBeta) {
	std::random_device rd;  // 将用于获得随机数引擎的种子
	std::mt19937_64 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
	std::uniform_real_distribution<float> alphaDis(startAlpha, endAlpha);
	std::uniform_real_distribution<float> betaDis(startBeta, endBeta);

	return Polar3DToXYZ({1, alphaDis(gen), betaDis(gen)});
}

Color getRandomColor() {
	std::random_device rd;  // 将用于获得随机数引擎的种子
	std::mt19937_64 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
	uint8_t colorClassCount = 72;
	std::uniform_int_distribution<> colorHDis{0, colorClassCount};
	std::uniform_real_distribution<float> colorSDis{0, 1};


	float H = colorHDis(gen) * 360 / colorClassCount;
	float S = 1;
	float V = 1;
	Color rgb = ColorFromHSV(H, S, V);
	return rgb;
}

std::vector<Vector3> getBallPos(Vector3 ballPos, float radius, uint8_t alphaCount, uint8_t betaCount) {
	float alpha = 90;
	float deltAlpha = 180. / alphaCount;
	float beta = 0.;
	float deltBeta = 360.0 / betaCount;
	std::vector<Vector3> res;
	for (int i = 0; i <= alphaCount; ++i) {
		for (int j = 0; j < betaCount; ++j) {

			Vector3 relatePos = Polar3DToXYZ((Polar3D) {
				radius, alpha, beta
			});
			Vector3 pointPos = Vector3Add(ballPos, relatePos);
			res.push_back(pointPos);

			beta += deltBeta;
		}
		alpha -= deltAlpha;
	}
	return res;
}

std::vector<Vector3> getSpeedlistFromPoslist(Vector3 corePos, float speedNorm, const std::vector<Vector3>& posList) {
	std::vector<Vector3> res;
	for (Vector3 pointPos : posList) {
		Vector3 direction = Vector3Normalize(Vector3Subtract(pointPos, corePos));
		Vector3 speed = Vector3Scale(direction, speedNorm);
		res.push_back(speed);
	}
	return res;
}

std::vector<Vector3> getSpeedListFormPoslistWithRatio(Vector3 corePos, float ratio, const std::vector<Vector3>& posList) {
	std::vector<Vector3> res;
	for (Vector3 pointPos : posList) {
		Vector3 direction = Vector3Normalize(Vector3Subtract(pointPos, corePos));
		float distance = Vector3Distance(pointPos, corePos);
		Vector3 speed = Vector3Scale(direction, distance * ratio);
		res.push_back(speed);
	}
	return res;
}

bool isToDo(float iterRatio) {
	std::random_device rd;  // 将用于获得随机数引擎的种子
	std::mt19937_64 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
	std::uniform_real_distribution<float> iterIsDis(0, 1);
	if (iterIsDis(gen) > iterRatio) {
		return true;
	} else {
		return false;
	}
}

std::vector<Vector3> getHeartPos(Vector3 heartPos, Vector3 normal, float radius, uint8_t pointCount) {
	std::vector<Vector3> res;
	return res;
}

